// import axios from 'axios'
import { safeToNumber } from '@/utils/LodashUtils.js'
import _ from 'lodash'
import { usePriceSummaryStore } from '@/stores/index.js'
import { ingredientListSuper } from '@/promises/ingredientListFn.js'
const priceSummaryStore = usePriceSummaryStore()
const { getVal } = priceSummaryStore

export const defineIngredientListTFn = (data) => {
  data.ingredientListT = () => ingredientListSuper(data)
}

export const setData = async (data) => {
  // const res = await axios.get('http://localhost:3000/raw')
  // data.raw = res.data
  data.raw = data.raw.filter(item => getVal(item, 'contract') === data.contract)
  // console.log(data.raw)
  let result = {}
  const getFn = (key, data) => {
    switch (key) {
      case 'factory': {
        result = getVal(data.raw[0], key)
        break
      }
      case 'ingredients': {
        const groupByResult = _.groupBy(data.raw, item => getVal(item, 'weft') + getVal(item,'ingredient'))
        if (_.isEmpty(groupByResult)) {
          result = []
          break
        }
        const ingredients = _.map(groupByResult, (v, k) => {
          const total = v.reduce((prev, cur) => prev + safeToNumber(getVal(cur, 'weftCost')) + safeToNumber(getVal(cur, 'deliveryCost')), 0)
          if (total) {
            return {
              ingredient: k,
              maker: getVal(v[0], 'weftFactory'),
              total
            }
          }
        })
        // console.log(ingredients)
        result = ingredients
        break
      }
    }
    data[key] = result
  }

  getFn('factory', data)
  getFn('ingredients', data)

  return data

  // data.ingredients = [
  //   { ingredient: '2/75D 涤纶丝', maker: '柯珞克1', total: '10000' },
  //   { ingredient: '2/16S 60棉/40晴膨体', maker: '柯珞克2', total: '20000' },
  //   { ingredient: '1.8倍 氨纶', maker: '柯珞克3', total: '30000' },
  //   { ingredient: '1/140D 涤纶丝', maker: '柯珞克4', total: '40000' }
  // ]
}